package com.msh.artascope.devcloud.service.wrapper;

import com.msh.artascope.devcloud.client.enums.biz.GitlabConfigTypeEnum;
import com.msh.artascope.devcloud.client.po.GitlabConfigPo;
import com.msh.artascope.devcloud.service.client.GiteaClient;
import com.msh.artascope.devcloud.service.client.GitlabClient;
import com.msh.frame.client.exception.BizException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class GitWrapper {
    private final GitlabClient gitlabClient;
    private final GiteaClient giteaClient;

    public Long getProjectId(GitlabConfigPo po, String url) {
        Long projectId = null;
        if(GitlabConfigTypeEnum.GITEA.getCode().equals(po.getType())){
            projectId = giteaClient.getProjectId(po, url);
        }else if(GitlabConfigTypeEnum.GITLAB.getCode().equals(po.getType())){
            projectId = gitlabClient.getProjectId(po, url);
        }
        BizException.doThrowIfNull("无法获取项目id", projectId);
        return projectId;
    }

    public String getLastCommitId(GitlabConfigPo po, Long projectId, String branch) {
        String commitId = null;
        if(GitlabConfigTypeEnum.GITEA.getCode().equals(po.getType())){
            commitId = giteaClient.getLastCommitId(po, projectId, branch);
        }else if(GitlabConfigTypeEnum.GITLAB.getCode().equals(po.getType())){
            commitId = gitlabClient.getLastCommitId(po, projectId, branch);
        }
        BizException.doThrowIfNull("无法获取项目最后一次提交id", commitId);
        return commitId;
    }
}
